import wx from "weixin-js-sdk";
import store from '../../vuex/store';
import Vue from "vue";

export default {
    shareLink: "",
    shareimgUrl: store.state.domainUrl+"wxapi/amxlloge.jpg",
    sharetitle: "美容模板",
    shareDesc: "让每个美容院都用上我们的模板",
    getWxData(qrcodes) {
      
      if(store.state.sistem=='ios'){
        this.shareLink=store.state.iosPath;
      }else this.shareLink = window.location.href;

      this.shareimgUrl = store.state.soursePathTwo + store.state.storeInfo.icon;
      this.sharetitle =  store.state.storeInfo.name;
      this.shareDesc = store.state.storeInfo.name;

      var shareurl = store.state.shareUrl;//'https://www.jubenxiong.com/';
      // alert('this.shareLink='+this.shareLink);
      console.log('this.shareLink='+this.shareLink);
      Vue.prototype.$http
        .post(shareurl+"wxapi/share.php", {
          urls:this.shareLink
        })
        .then(response => {
          let data = response.body;
          var wxob = {};

          wxob.timestamp = data.timestamp;
          wxob.nonceStr = data.nonceStr;
          wxob.signature = data.signature;
          wxob.link = this.shareLink;
          wxob.imgUrl = this.shareimgUrl;
          wxob.title = this.sharetitle;
          wxob.desc = this.shareDesc;
          this.setData(wxob, qrcodes);

          // alert('this.shareLink='+  this.shareLink);
        })
        .catch(function(response) {
          console.log(response);
          // alert(response);
        });
    },
    setData(obj, selfobj) {
      wx.config({
        debug: false,
        appId: store.state.wxappid,
        timestamp: obj.timestamp,
        nonceStr: obj.nonceStr,
        signature: obj.signature,
        jsApiList: [
          // 所有要调用的 API 都要加到这个列表中          
          "checkJsApi",
          "scanQRCode"
        ]
      });

      wx.ready(function () {
        wx.checkJsApi({  
          jsApiList : ['scanQRCode'],  
          success : function(res) {  
          }  
        });

        wx.error(function(res){
          //console.log("res=".res);
          // alert('error='+res.errMsg);
        });

      });
    },
    wxscanQRCode(qrend) {
       console.log('ent S');
      wx.scanQRCode({
        needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
        scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
        success: function(res) {
          var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
          //document.getElementById("wm_id").value = result;//将扫描的结果赋予到jsp对应值上
          //alert("扫描成功::扫描码=" + result);
          qrend.QrCodeEnd(result);
        },
        fail:function(res){
        }
      });
    }
};
